﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScratchCard.AppCode;

namespace ScratchCard.controller
{
    /// <summary>
    /// scrath 的摘要说明
    /// </summary>
    public class scrath : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";

            string action = context.Request.QueryString["action"];
            if (string.IsNullOrEmpty(action)) return;

            try
            {
                string json = string.Empty;
                switch (action)
                {
                    case "getpriceurl":
                        json = GetPrizeUrl(context);
                        context.Response.Write(json);
                        break;
                    case "saveprizesetting":
                        SavePrizeSetting(context);
                        break;

                    case "getprizesetting":
                        json = GetPrizeSetting();
                        context.Response.Write(json);
                        break;
                    case "getprizelist":
                        json = GetPrizeList();
                        context.Response.Write(json);
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(string.Format("执行{0}错误,错误信息为:{1}.", action, ex.Message));
            }
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        private string GetPrizeUrl(HttpContext context)
        {
            var validtimes = int.Parse(context.Request.QueryString["validtimes"]);

            var url = GetLuckyDraw(validtimes);

            var urlPrice = new
            {
                url
            };

            return JsonHelper.SerializeObject(urlPrice);
        }


        /// <summary>
        /// 保存设置信息
        /// </summary>
        private void SavePrizeSetting(HttpContext context)
        {
            var starttime = context.Request.Form["starttime"];
            var endtime = context.Request.Form["endtime"];
            var introductions = context.Request.Form["introductions"];
            var interpretation = context.Request.Form["interpretation"];
            var awards = context.Request.Form["awards"];

            var model = new PrizeSettingModel(starttime, endtime, introductions, interpretation, awards);

            model.Save();
        }

        /// <summary>
        /// 获取设置信息
        /// </summary>
        private string GetPrizeSetting()
        {
            var m = PrizeSettingModel.Create();
            return JsonHelper.SerializeObject(m);
        }

        /// <summary>
        /// 获取待发列表
        /// </summary>
        private string  GetPrizeList()
        {
            return LuckyBox.Instance.GetPrizeReleaseModels();
        }

        //每个设备的在活动期间可抽奖次数
        int luckycount = 1;

        /// <summary>
        /// 获取幸运图片
        /// </summary>
        private string GetLuckyDraw(int validtimes)
        {
            var price = "thankyou";

            if(validtimes< luckycount)  //有效次数小于1 的时候，是可抽取的。
            {
                var prize = LuckyBox.Instance.WinLottery(DateTime.Now, "abc");

                switch (prize)
                {
                    case LuckyBox.Prize.Grand:
                        price = "grandprize";
                        break;
                    case LuckyBox.Prize.First:
                        price = "firstprize";
                        break;
                    case LuckyBox.Prize.Second:
                        price = "secondprize";
                        break;
                    case LuckyBox.Prize.Third:
                        price = "thirdprize";
                        break;
                    case LuckyBox.Prize.Forth:
                        price = "forthprize";
                        break;
                    case LuckyBox.Prize.Fifth:
                        price = "fifthprize";
                        break;
                }
            }

            return string.Format("../assets/images/{0}.png", price);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}